package hu.mapro.gwtui.gxt.client.recordseditor.requestfactory.entity;

import hu.mapro.gwtui.client.Action;
import hu.mapro.gwtui.client.data.EntityRequestMethods;
import hu.mapro.gwtui.gxt.client.recordseditor.requestfactory.Interface;
import hu.mapro.gwtui.gxt.client.recordseditor.requestfactory.RequestContextHolderInterface;

import java.util.List;

import com.google.web.bindery.requestfactory.shared.EntityProxy;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class Adapter<T extends EntityProxy> implements
		Interface<T> {
	
	RequestContextHolderInterface<EntityRequestMethods<T>> requestContextHolder;
	
	public Adapter(
			RequestContextHolderInterface<EntityRequestMethods<T>> requestContextHolder) {
		super();
		this.requestContextHolder = requestContextHolder;
	}

	@Override
	public EntityRequestMethods<T> newRequestContext() {
		requestContextHolder.newRequestContext();
		return requestContextHolder.getCurrentRequestContext();
	}

	@Override
	public void list(Receiver<List<T>> receiver) {
		requestContextHolder.getCurrentRequestContext().list().fire(receiver);
	}

	@Override
	public void persist(T object, Receiver<Void> receiver) {
		requestContextHolder.getCurrentRequestContext().persist(object).fire(receiver);
	}

	@Override
	public void merge(T object, Receiver<T> receiver) {
		requestContextHolder.getCurrentRequestContext().merge(object).fire(receiver);
	}

	@Override
	public void remove(List<T> object, Receiver<Void> receiver) {
		for (T o : object) {
			requestContextHolder.getCurrentRequestContext().remove(o);
		}
		
		requestContextHolder.getCurrentRequestContext().fire(receiver);
	}

	@Override
	public void addChangeHandler(Action handler) {
	}
}